package com.it.springbootcondition.condition;

import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.util.MultiValueMap;

import java.util.Map;
//import redis.clients.jedis.Jedis;

public class ClassCondition implements Condition {
    /**

     * @param conditionContext 上下文对象，用于获取环境，ioc容器，ClassLoader对象
     * @param annotatedTypeMetadata 注解的原对象，可以用于获取注解定义的属性值
     * @return
     */

    @Override
    public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
        //需求：导入jdeis坐标后创建Bean
        //判断redis.clients.jedis.Jedis是否存在
       /* boolean flag = true;
        try {
            Class<?> aClass = Class.forName("redis.clients.jedis.Jedis");
        } catch (ClassNotFoundException e) {
            flag = false;
        }
        return flag;*/

        //需求：导入通过注解属性值value指定坐标后创建Bean
        //获取注解属性值 value

        Map<String, Object> map = annotatedTypeMetadata.getAnnotationAttributes(ConditionOnClass.class.getName());
        //System.out.println(map);
        String[] value = (String[])map.get("value");

        boolean flag = true;
        try {
            for (String classname : value) {
                Class<?> aClass = Class.forName(classname);
            }

        } catch (ClassNotFoundException e) {
            flag = false;
        }
        return flag;
    }
}
